package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.management.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理控制器
 *
 * @Author:Mr.m
 * @Date: 2025-09-29 10:53
 * @Description: TODO
 */
@Slf4j
@RestController
//url路径窄化
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

//    三大步骤   ① 拉架子   ② 补全参数和返回值  ③  代码

    //查询所有部门
    @GetMapping("/list")
    public Result list() {
        List<Dept> depts = deptService.list();
        return Result.success(depts);
    }

    /*
    * 删除部门
    * */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("删除部门id:{}",id);
        deptService.deleteById(id);
        return Result.success();
    }


    /**
     * 修改部门
     *
     * @param dept
     * @return Result
     */
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改部门:{}",dept);
        deptService.update(dept);
        return Result.success();
    }


    /*
    * 根据id查询部门
    * */
    @GetMapping("/{id}")
    public Result queryDeptById(@PathVariable("id") Integer id) {
        log.info("查询部门id:{}",id);
        Dept dept = deptService.queryDeptById(id);
        return Result.success(dept);
    }

    /*
     * 根据页面条件查询部门
     * */
    @GetMapping
    public Result queryDeptByPage(
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "status", required = false) Integer status,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        log.info("查询部门,参数:name:{},status:{},page:{},pageSize:{}", name, status, page, pageSize);
        PageResult<Dept> pageResult = deptService.page(name, status, page, pageSize);
        return Result.success(pageResult);
    }


    /*
     * 新增部门
     * */
    @PostMapping
    public Result addDept(@RequestBody Dept dept) {
        log.info("新增部门:{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }



}
